package com.haiheng.videodoorbell.widget.webview

import android.content.Context
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.webkit.JavascriptInterface
import androidx.annotation.Keep
import com.google.gson.Gson
import com.haiheng.library.log.LogUtils
import com.haiheng.library_base.view.webview.H5Response
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView

/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2022/3/25 0025
 * @Version: 1.0
 */
class X5WebView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : WebView(context, attrs){

    val TAG : String  = "X5WebView"
    private val BRIDGE_NAME = "_dsbridge"
    private val innerJavascriptInterface: InnerJavascriptInterface =
        InnerJavascriptInterface()
    private val mainHandler = Handler(Looper.getMainLooper())

    init {
//        this.webViewClient = MyX5WebViewClient(this)
//        this.webChromeClient = MyX5WebChromeClient(this)
        initWebViewSettings()
        this.view.isClickable = true
    }

    private fun initWebViewSettings() {
        val webSetting = this.settings
        settings.setSupportZoom(true);

        settings.setUseWideViewPort(true);  // 将图片调整到适合 WebView 的大小
        settings.setLoadWithOverviewMode(true); // 缩放至屏幕大小

//        webSetting.setBuiltInZoomControls(true);
//
//        webSetting.setDisplayZoomControls(false);

        webSetting.javaScriptEnabled = true
        webSetting.javaScriptCanOpenWindowsAutomatically = true
        webSetting.allowFileAccess = true
        webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webSetting.setSupportZoom(true)
        webSetting.builtInZoomControls = true
        webSetting.useWideViewPort = true
        webSetting.setSupportMultipleWindows(true)
        // 网页内容的宽度自适应屏幕
        webSetting.loadWithOverviewMode = true
        webSetting.setAppCacheEnabled(true)
        // webSetting.setDatabaseEnabled(true);
        webSetting.domStorageEnabled = true
        webSetting.setGeolocationEnabled(true)
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE)
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.pluginState = WebSettings.PluginState.ON_DEMAND
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.cacheMode = WebSettings.LOAD_NO_CACHE
        settings.setMediaPlaybackRequiresUserGesture(false);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
            super.addJavascriptInterface(innerJavascriptInterface, BRIDGE_NAME)
        } else {
            // add bridge tag in lower android version
            webSetting.userAgentString = webSetting.userAgentString + " _dsbridge"
        }
    }





    fun runOnMainThread(runnable: Runnable) {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            runnable.run()
            return
        }
        mainHandler.post(runnable)
    }

    internal class InnerJavascriptInterface {

        @Keep
        @JavascriptInterface
        fun call(methodName: String, argStr: String): String {
            LogUtils.e(" h5调用  methodName"+methodName+"__内容："+argStr)
            if (methodName == "jsBridgeToCallAppFunc") {
                val h5BaseResponse: H5Response =
                    Gson().fromJson(argStr, H5Response::class.java)


            }
            return argStr
        }
    }


}